import time
from Common.Base import Base, open_browser
from time import sleep

register_url = 'http://172.16.11.202:8080/ecshop/user.php?act=register'


class RegisterPage(Base):
    """制作定位器"""
    username_loc = ('id', 'username')  # 用户名输入框
    username_notice_loc = ('id', 'username_notice')  # 用户名输入框提示
    email_loc = ('id', 'email')  # email输入框
    email_notice_loc = ('id', 'email_notice')  # email输入框提示
    password_loc = ('id', 'password1')  # 密码输入框
    password_loc_notice = ('id', 'password_notice')  # 密码输入框提示
    confirm_password_loc = ('id', 'conform_password')  # 确认密码输入框
    confirm_password_notice_loc = ('id', 'conform_password_notice')  # 确认密码输入框提示
    qq_loc = ('name', 'extend_field2')  # QQ输入框
    office_num_loc = ('name', 'extend_field3')  # 办公电话输入框
    family_num_loc = ('name', 'extend_field4')  # 家庭电话输入框
    phone_num_loc = ('name', 'extend_field5')  # 手机输入框
    password_question_loc = ('css selector', 'select[name="sel_question"] option:nth-of-type(2)')  # 密码提示问题选择我最好朋友的生日？
    password_answer_loc = ('name', 'passwd_answer')  # 密码问题答案输入框
    agreement_loc = ('name', 'agreement')  # 我已看过并接受《用户协议》
    submit_loc = ('name', 'Submit')  # 立即注册
    user_agreement_loc = ('link text', '用户协议')  # 用户协议链接
    login_loc = ('link text', '我已有账号，我要登录')  # 我有账号,我要登录
    forget_password_loc = ('css selector', 'a[href*="user.php?act=get_password"]')  # 您忘记密码了吗？
    result_loc = ('class name', 'f4_b')  # 注册后页面显示的用户名

    """封装操作层"""

    def input_username(self, username):
        """输入用户名"""
        self.send_keys(self.username_loc, username)

    def get_username_notice(self):
        """获取用户名提示"""
        time.sleep(4)
        return self.get_element_text(self.username_notice_loc)

    def input_email(self, email):
        """输入email"""
        self.send_keys(self.email_loc, email)

    def get_email_notice(self):
        """获取email提示"""
        time.sleep(4)
        return self.get_element_text(self.email_notice_loc)

    def input_password(self, password):
        """
        输入密码
        @param password: 密码
        @return:
        """
        self.send_keys(self.password_loc, password)

    def get_password_notice(self):
        """获取密码提示信息"""
        time.sleep(4)
        return self.get_element_text(self.password_loc_notice)

    def conform_password(self, password):
        """
        输入确认密码
        @param password: 确认密码
        @return:
        """
        self.send_keys(self.confirm_password_loc, password)

    def get_conform_password_notice(self):
        """获取确认密码输入框提示信息"""
        time.sleep(4)
        return self.get_element_text(self.confirm_password_notice_loc)

    def input_qq(self, QQ):
        """
        输入QQ
        @param QQ: QQ号码
        @return:
        """
        self.send_keys(self.qq_loc, QQ)

    def input_office_num(self, office_num):
        """
        输入办公电话
        @param office_num: 办公电话
        @return:
        """
        self.send_keys(self.office_num_loc, office_num)

    def input_family_num(self, family_num):
        """
        输入家庭电话
        @param family_num: 家庭电话
        @return:
        """
        self.send_keys(self.family_num_loc, family_num)

    def input_phone_num(self, phone_num):
        """
        输入手机号
        @param phone_num: 手机号
        @return:
        """
        self.send_keys(self.phone_num_loc, phone_num)

    def select_password_question(self):
        """
        选择密码提示问题
        @return:
        """
        self.click(self.password_question_loc)

    def input_password_answer(self, answer):
        """
        输入密码问题答案
        @param answer: 密码问题答案
        @return:
        """
        self.send_keys(self.password_answer_loc, answer)

    def click_agreement(self):
        """
        勾选我已看过并接受《用户协议》
        :return:
        """
        checkbox = self.driver.find_element_by_name('agreement')
        if checkbox.is_selected():
            pass
        else:
            self.click(self.agreement_loc)

    def user_agreement(self):
        """
        用户协议
        @return:
        """
        self.click(self.user_agreement_loc)

    def js_down(self):
        """
        向下滚动屏幕
        :return:
        """
        js = "window.scrollTo(0,1000)"
        self.driver.execute_script(js)

    def register_submit(self):
        """
        立即注册按钮
        @return:
        """
        self.click(self.submit_loc)

    def login(self):
        """
        已有账号去登录
        @return:
        """
        self.click(self.login_loc)

    def forget_password(self):
        """
        忘记密码
        @return:
        """
        self.click(self.forget_password_loc)

    def is_register_success(self, username):
        """
        判断注册是否成功,如果成功,返回TRUE,反之返回False
        @return:
        """
        return self.is_text_equal_in_element(self.result_loc, username)

    def get_register_username(self):
        """
        获取注册后的用户名
        @return:
        """
        sleep(3)
        return self.get_element_text(self.result_loc)

    def is_click_success(self, url):
        """
        判断是否链接跳转成功
        @param url: 原窗口url
        @return:
        """
        return self.is_url_equal(url)


if __name__ == '__main__':
    login = RegisterPage(open_browser())  # 打开浏览器
    login.open_url(register_url)  # 进入登录首页
    # login.input_username('Kerry')  # 输入用户名
    # login.input_email('5432243@163.com')  # 输入邮箱
    # login.input_password('123456')  # 输入密码
    # login.input_confirm_password('123456')  # 确认密码
    # login.input_qq('2345678')  # 输入qq
    # login.input_office_tel('1234567')  # 输入办公电话
    # login.input_home_tel('1234567')  # 输入家庭电话
    # login.input_phone_number('13845678901')  # 输入手机号码
    # login.click_sel_question(1)  # 选择密码提示问题
    # login.input_passwd_answer('20210102')  # 输入密码提示问题答案
    # login.click_agreement()  # 勾选我已看过并接受《用户协议》复选框
    # login.js_down()  # 屏幕向下滚动
    # login.click_submit()  # 点击注册按钮
    # login.click_login()  # 点击我已有账号，我要登录
    # login.click_forget_password()  # 点击您忘记密码了吗？
    # RegisterPage.close_browser(login)  # 关闭浏览器
